<?php
/**
 * Created by PhpStorm.
 * User: FredGui
 * Date: 2017/1/9
 * Time: 19:12
 */

namespace Gmm\App\Admin;

use gmm\apps\core\PhalBaseVolt;
use Phalcon\Cli\Dispatcher;
use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\DiInterface;
use Phalcon\Mvc\ModuleDefinitionInterface;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;

class Module implements ModuleDefinitionInterface
{
    /**
     * Registers the module auto-loader
     *
     * @param DiInterface $di
     */
    public function registerAutoloaders(DiInterface $di = null)
    {
        $loader = new Loader();
        
        $loader->registerNamespaces(
            [
                'app\mvc\home\controllers' => __DIR__ . "/controllers/",
                'apps\mvc\home\models'      => __DIR__ . "/models/",
            ]
        );

        $loader->register();
    }

    /**
     * Registers the module-only services
     *
     * @param DiInterface $di
     */
    public function registerServices(DiInterface $di)
    {
        /**
         * Read configuration
         */
        $config = include __DIR__ . "/config/config.php";


        $di -> setShared('view',function()use($config){
            $view = new \Phalcon\Mvc\View();
            
            $view->setViewsDir(__DIR__ . "/views/");

            $view -> registerEngines(array(
                '.phtml' => function($view, $di)  {
                    //$volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
                    $volt = new PhalBaseVolt($view,$di);
                    $volt -> setOptions(array(
                        'compileAlways' => true,
                        'compiledPath'  =>  ROOT_PATH . '/apps/cache/compiled/frontend/',
                    ));
                    $volt -> initFunction();
                    return $volt;
                },
            ));
            return $view;
        });
        
//        /**
//         * Setting up the view component
//         */
//        $di["view"] = function () {
//            $view = new View();
//
//            $view->setViewsDir(
//                __DIR__ . "/views/"
//            );
//            $view->registerEngines(
//                [
//                    ".phtml" => VoltEngine::class,
//                ]
//            );
//            return $view;
//        };

        /**
         * Database connection is created based in the parameters defined in the
         * configuration file
//         */
//        $di["db"] = function () use ($config) {
//            return new DbAdapter(
//                [
//                    "host"     => $config->database->host,
//                    "username" => $config->database->username,
//                    "password" => $config->database->password,
//                    "dbname"   => $config->database->name,
//                ]
//            );
//        };
    }
}
